package com.xing.activity;

import com.xing.fragment.AboutFragment;
import com.xing.fragment.ManageFragment;
import com.xing.fragment.SettingFragment;
import com.xing.fragment.SurveyListFragment;
import com.xing.fragment.UploadFragment;
import com.xing.questionnairesurvey.R;
import com.xing.util.Common;
import com.xing.util.FragmentNameFlagList;
import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public class HomeActivity extends FragmentActivity{
	
	private LinearLayout surveyLayout;
	private LinearLayout managerLayout;
	private LinearLayout uploadLayout;
	private LinearLayout settingLayout;
	private LinearLayout aboutLayout;
	
	private TextView surveytextTextView;
	private TextView managertextTextView;
	private TextView uploadtextTextView;
	private TextView settingtextTextView;
	private TextView abouttextTextView;
	
	private Drawable bottomNavigationDrawableOn;
	
	private static FragmentManager fragmentManager;
	
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_home);
		
		bottomNavigationDrawableOn = getResources().getDrawable(R.drawable.bottom_navigation_on);
		fragmentManager = this.getSupportFragmentManager();
		
		addSurveyFragement();
		
		initWidgets();
		
	}
	
	
	
	private void initWidgets(){
		surveyLayout = (LinearLayout) findViewById(R.id.survey);
		managerLayout = (LinearLayout)findViewById(R.id.manage);
		uploadLayout = (LinearLayout) findViewById(R.id.upload);
		settingLayout = (LinearLayout) findViewById(R.id.setting);
		aboutLayout = (LinearLayout)findViewById(R.id.about);
		
		surveytextTextView = (TextView) findViewById(R.id.survey_textview);
		managertextTextView = (TextView) findViewById(R.id.manage_textview);
		uploadtextTextView= (TextView) findViewById(R.id.upload_textview);
		settingtextTextView = (TextView) findViewById(R.id.setting_textview);
		abouttextTextView = (TextView) findViewById(R.id.about_textview);
		
		surveyLayout.setOnClickListener(myListener);
		managerLayout.setOnClickListener(myListener);
		uploadLayout.setOnClickListener(myListener);
		settingLayout.setOnClickListener(myListener);
		aboutLayout.setOnClickListener(myListener);
		
	}
	
	private OnClickListener myListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.survey:
				showSurveyFragement();
				bottomNavigatoinShow(surveytextTextView);
				break;
			case R.id.manage:
				showManageFragement();
				bottomNavigatoinShow(managertextTextView);
				break;
			case R.id.upload:
				showUploadFragement();	
				bottomNavigatoinShow(uploadtextTextView);
				break;
			case R.id.setting:
				showSettingFragement();
				bottomNavigatoinShow(settingtextTextView);
				break;
			case R.id.about:
				showAboutFragement();
				bottomNavigatoinShow(abouttextTextView);
				break;
			default:
				break;
			}
		}
	};
	
	public  void bottomNavigatoinShow(TextView textView){
		switch (textView.getId()) {
		case R.id.survey_textview:
			surveytextTextView.setBackgroundDrawable(bottomNavigationDrawableOn);
			managertextTextView.setBackgroundDrawable(null);
			uploadtextTextView.setBackgroundDrawable(null);
			settingtextTextView.setBackgroundDrawable(null);
			abouttextTextView.setBackgroundDrawable(null);
			break;
		case R.id.manage_textview:
			surveytextTextView.setBackgroundDrawable(null);
			managertextTextView.setBackgroundDrawable(bottomNavigationDrawableOn);
			uploadtextTextView.setBackgroundDrawable(null);
			settingtextTextView.setBackgroundDrawable(null);
			abouttextTextView.setBackgroundDrawable(null);
			break;
		case R.id.upload_textview:
			surveytextTextView.setBackgroundDrawable(null);
			managertextTextView.setBackgroundDrawable(null);
			uploadtextTextView.setBackgroundDrawable(bottomNavigationDrawableOn);
			settingtextTextView.setBackgroundDrawable(null);
			abouttextTextView.setBackgroundDrawable(null);
			break;
		case R.id.setting_textview:
			surveytextTextView.setBackgroundDrawable(null);
			managertextTextView.setBackgroundDrawable(null);
			uploadtextTextView.setBackgroundDrawable(null);
			settingtextTextView.setBackgroundDrawable(bottomNavigationDrawableOn);
			abouttextTextView.setBackgroundDrawable(null);
			break;
		case R.id.about_textview:
			surveytextTextView.setBackgroundDrawable(null);
			managertextTextView.setBackgroundDrawable(null);
			uploadtextTextView.setBackgroundDrawable(null);
			settingtextTextView.setBackgroundDrawable(null);
			abouttextTextView.setBackgroundDrawable(bottomNavigationDrawableOn);
			break;
		default:
			break;
		}
		
	}
	
	private void addSurveyFragement(){
		FragmentTransaction ft = fragmentManager.beginTransaction();
		SurveyListFragment suryFragment = new SurveyListFragment();
		ft.add(R.id.fragmentRoot, suryFragment, FragmentNameFlagList.SURVEY);
		ft.addToBackStack( FragmentNameFlagList.SURVEY);
		ft.commit();
	}
	
	private void showSurveyFragement(){
		if(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY)!=null && fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY).isVisible()) {
			return;
		}
		popAllFragmentsExceptTheBottomOne();
	}
	
	private void showManageFragement(){
		if(fragmentManager.findFragmentByTag(FragmentNameFlagList.MANAGE)!=null && fragmentManager.findFragmentByTag(FragmentNameFlagList.MANAGE).isVisible()) {
			return;
		}
		popAllFragmentsExceptTheBottomOne();
		ManageFragment manageFragment = new ManageFragment();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.hide(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY));
		fragmentTransaction.add(R.id.fragmentRoot, manageFragment, FragmentNameFlagList.MANAGE);
		fragmentTransaction.addToBackStack(FragmentNameFlagList.MANAGE);
		fragmentTransaction.commit();
		
	}
	
	private void showUploadFragement(){
		if(fragmentManager.findFragmentByTag(FragmentNameFlagList.UPLOAD)!=null && fragmentManager.findFragmentByTag(FragmentNameFlagList.UPLOAD).isVisible()) {
			return;
		}
		popAllFragmentsExceptTheBottomOne();
		UploadFragment uploadFragment = new UploadFragment();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.hide(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY));
		fragmentTransaction.add(R.id.fragmentRoot, uploadFragment, FragmentNameFlagList.UPLOAD);
		fragmentTransaction.addToBackStack(FragmentNameFlagList.UPLOAD);
		fragmentTransaction.commit();
	}
	
	private void showSettingFragement(){
		if(fragmentManager.findFragmentByTag(FragmentNameFlagList.SETTING)!=null && fragmentManager.findFragmentByTag(FragmentNameFlagList.SETTING).isVisible()) {
			return;
		}
		popAllFragmentsExceptTheBottomOne();
		SettingFragment settingFragment = new SettingFragment();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.hide(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY));
		fragmentTransaction.add(R.id.fragmentRoot, settingFragment, FragmentNameFlagList.SETTING);
		fragmentTransaction.addToBackStack(FragmentNameFlagList.SETTING);
		fragmentTransaction.commit();
		
	}
	
	private void showAboutFragement(){
		if(fragmentManager.findFragmentByTag(FragmentNameFlagList.ABOUT)!=null && fragmentManager.findFragmentByTag(FragmentNameFlagList.ABOUT).isVisible()) {
			return;
		}
		popAllFragmentsExceptTheBottomOne();
		AboutFragment aboutFragment = new AboutFragment();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.hide(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY));
		fragmentTransaction.add(R.id.fragmentRoot, aboutFragment, FragmentNameFlagList.ABOUT);
		fragmentTransaction.addToBackStack(FragmentNameFlagList.ABOUT);
		fragmentTransaction.commit();
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY)!=null && fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY).isVisible()) {
				try {
					this.finish();
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	/**
	 * 从back stack弹出所有的fragment，保留首页的那个
	 */
	public static void popAllFragmentsExceptTheBottomOne() {
		try {
			for(int i=0,count=fragmentManager.getBackStackEntryCount()-1; i<count; i++) {
				fragmentManager.popBackStack();
			}
		}catch(Exception e) {
			
		}
	}

}
